<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-form-item label="消息标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入消息标题"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="发送时间" prop="senderTime">
            <el-date-picker clearable size="small"
                            v-model="queryParams.senderTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择发送时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="发布状态" prop="sendState">
            <el-select v-model="queryParams.sendState" placeholder="请选择发布状态" clearable size="small">
              <el-option
                v-for="item in dict.send_state"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['notice:noticeSiteMessage:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['notice:noticeSiteMessage:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['notice:noticeSiteMessage:remove']">删除
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-plus" size="mini" :disabled="multiple" @click="handlePublish">发布
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleCall">撤回
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeSiteMessageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="消息标题" align="left" prop="title"/>
      <el-table-column label="发送者姓名" align="center" prop="senderName"/>
      <el-table-column label="发送时间" align="center" prop="senderTime" width="180"/>
      <el-table-column label="消息类型" align="center" prop="messageType">
        <template slot-scope="scope">
          {{ dict.label.message_type[scope.row.messageType] }}
        </template>
      </el-table-column>
      <el-table-column label="接收类型" align="center" prop="receiveType">
        <template slot-scope="scope">
          {{ dict.label.receive_type[scope.row.receiveType] }}
        </template>
      </el-table-column>
      <el-table-column label="发布状态" align="center" prop="sendState">
        <template slot-scope="scope">
          {{ dict.label.send_state[scope.row.sendState] }}
        </template>
      </el-table-column>
      <el-table-column label="撤回时间" align="center" prop="cancelTime" width="180">
        <template slot-scope="scope">
          <span v-if="scope.row.sendState=='30'">{{ scope.row.cancelTime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <NoticeSiteMessageForm ref="formRef" @cancel="getList"/>
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listNoticeSiteMessage, delNoticeSiteMessage, msgPublish,msgRecall} from "@/api/notice/noticeSiteMessage";
  import NoticeSiteMessageForm from "./form.vue"

  export default {
    name: "NoticeSiteMessage",
    components: {NoticeSiteMessageForm},
    mixins: [listMixin],
    dicts: ['message_type', 'receive_type', 'send_state',],
    data() {
      return {
        // 站内消息表格数据
        noticeSiteMessageList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          content: null,
          senderId: null,
          senderName: null,
          senderTime: null,
          messageType: null,
          receiveType: null,
          sendState: null,
          efficientTime: null,
          cancelTime: null,
        },
        selection:[]
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询站内消息列表 */
      getList() {
        this.loading = true;
        //只看自己发布的消息
        this.queryParams.senderId = this.$store.state.user.name || -1
        listNoticeSiteMessage(this.queryParams).then(response => {
          this.noticeSiteMessageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.selection = selection
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.$refs.formRef.show({"type": "ADD"});
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        const id = row.id || this.ids
        const option = {
          "type": "UPDATE",
          "id": id
        }
        this.$refs.formRef.show(option);
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        let msg = []
        for(let i in this.selection){
          let row = this.selection[i]
          if(row.sendState != '10'){
            msg.push(`${row.title},不是草稿状态!`)
          }
        }
        if(msg.length > 0){
          this.msgWarning(msg.join(","))
          return
        }
        this.$confirm('是否确认删除选择的消息吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delNoticeSiteMessage(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      handlePublish(){
        const ids = this.ids
        let msg = []
        for(let i in this.selection){
          let row = this.selection[i]
          if(!(row.sendState == '10' || row.sendState == '30')){
            msg.push(`${row.title},必须是草稿、撤回状态!`)
          }
        }
        if(msg.length > 0){
          this.msgWarning(msg.join(","))
          return
        }
        this.$confirm('是否确认发布选择的消息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return msgPublish(ids);
        }).then(response => {
          this.getList()
          this.msgSuccess("发布成功!")
        }).catch(() => {
        });
      },
      handleCall(){
        const ids = this.ids
        let msg = []
        for(let i in this.selection){
          let row = this.selection[i]
          if(row.sendState != '20'){
            msg.push(`${row.title},不是发布状态!`)
          }
        }
        if(msg.length > 0){
          this.msgWarning(msg.join(","))
          return
        }
        this.$confirm('是否确认撤回选择的消息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          return msgRecall(ids);
        }).then(response => {
          this.getList()
          this.msgSuccess("撤回成功!")
          this.$store.dispatch('ReadCount')
        }).catch(() => {
        });
      }
    }
  };
</script>
